<template>
  <div class="upload-container clearfix">
    <div class="image-preview" v-show="imageUrl.length">
      <div class="image-preview-wrapper" v-for="(img,index) in imageUrl">
        <img :src="img" />
        <div class="image-preview-action">
          <i @click="rmImage(index)" class="el-icon-delete"></i>
        </div>
      </div>
    </div>
    <el-upload
      class="image-uploader clearfix"
      :data="dataObj"
      drag
      :multiple="false"
      :show-file-list="false"
      accept
      action="http://upload.qiniu.com/"
      :on-success="handleImageScucess"
      :on-error="handleImageErr"
      :before-upload="beforeUpload"
    >
      <i class="el-icon-upload"></i>
      <div class="el-upload__text">
        <em>您也可以上传思考题答题图片</em>
      </div>
    </el-upload>
  </div>
</template>
<script>
import { getQnToken } from "api/question";
import { gethashcode, deepClone } from "common/utils";

export default {
  name: "singleImageUpload",
  props: {
    value: Array
  },
  computed: {
    imageUrl() {
      return this.value;
    }
  },
  data() {
    return {
      dataObj: { token: "", key: "" }
    };
  },
  methods: {
    rmImage(index) {
      console.log(111, index);
      let nImage = deepClone(this.value);
      nImage.splice(index, 1);
      this.$emit("updateImage", nImage);
    },
    emitInput(val) {
      let nImage = deepClone(this.value);
      nImage.push(val);
      this.$emit("updateImage", nImage);
    },
    handleImageScucess(file) {
      this.$notify({
        title: "成功",
        message: "上传图片成功",
        type: "success",
        duration: 2000
      });
      this.emitInput("http://cdn.gee4.cn/" + file.key);
    },
    handleImageErr() {
      this.$notify({
        title: " 失败",
        message: "上传图片失败",
        type: "error",
        duration: 2000
      });
    },

    beforeUpload(file) {
      const _self = this;
      let code = gethashcode();
      this.dataObj.key = code + file.name;
      return new Promise((resolve, reject) => {
        getQnToken()
          .then(response => {
            const token = response.data;
            _self._data.dataObj.token = token;
            resolve(true);
          })
          .catch(err => {
            console.log(err);
            reject(false);
          });
      });
    }
  }
};
</script>
<style lang="less">
.el-upload {
  display: block !important;
}

.el-upload-dragger {
  width: 100% !important;
}

.upload-container {
  position: relative;
  padding: 10px;

  .image-preview {
    width: 100%;
    position: relative;
    border: 1px dashed #d9d9d9;
    .image-preview-wrapper {
      position: relative;
      width: 100%;
      height: 100%;
      img {
        width: 100%;
        height: 100%;
      }
    }
    .image-preview-action {
      position: absolute;
      width: 100%;
      height: 100%;
      left: 0;
      top: 0;
      cursor: default;
      text-align: center;
      color: #fff;
      opacity: 0;
      font-size: 20px;
      background-color: rgba(0, 0, 0, 0.5);
      transition: opacity 0.3s;
      cursor: pointer;
      text-align: center;
      line-height: 200px;
      .el-icon-delete {
        font-size: 36px;
      }
    }
    &:hover {
      .image-preview-action {
        opacity: 1;
      }
    }
  }
  .image-app-preview {
    width: 100%;
    /*height: 180px;*/
    position: relative;
    border: 1px dashed #d9d9d9;
    float: left;
    margin-left: 50px;
    .app-fake-conver {
      height: 44px;
      position: absolute;
      width: 100%; // background: rgba(0, 0, 0, .1);
      text-align: center;
      line-height: 64px;
      color: #fff;
    }
  }
}
</style>